import { FormItemRule } from 'element-plus';
import { Component } from 'vue';

/**
 * 表单项类型枚举
 */
export enum FormItemType {
  INPUT = 'input',
  SELECT = 'select',
  DATE_PICKER = 'datePicker',
  RADIO_GROUP = 'radioGroup',
  CHECKBOX_GROUP = 'checkboxGroup',
  TREE_SELECT = 'treeSelect',
  NUMBER = 'number',
  SWITCH = 'switch',
  CASCADER = 'cascader',
  CUSTOM = 'custom',
}

/**
 * 表单项选项接口
 */
export interface FormItemOption {
  label: string;
  value: string | number | boolean;
  disabled?: boolean;
  children?: FormItemOption[]; // 级联选择器选项
}

/**
 * 表单项树节点接口
 */
export interface TreeNode {
  id: string | number;
  label: string;
  children?: TreeNode[];
  disabled?: boolean;
  isLeaf?: boolean;
}

/**
 * 日期选择器类型枚举
 */
export enum DatePickerType {
  DATE = 'date',
  DATETIME = 'datetime',
  WEEK = 'week',
  MONTH = 'month',
  YEAR = 'year',
  DATES = 'dates',
  DATERANGE = 'daterange',
  DATETIMERANGE = 'datetimerange',
  MONTHRANGE = 'monthrange',
}

/**
 * 表单项基础配置接口
 */
export interface BaseFormItemConfig {
  type: FormItemType;
  field: string;
  label: string;
  placeholder?: string;
  defaultValue?: any;
  rules?: FormItemRule | FormItemRule[];
  disabled?: boolean;
  hidden?: boolean;
  clearable?: boolean;
  required?: boolean;
  colSpan?: number; // 栅格占据的列数
  order?: number; // 排序，数值越小越靠前
  props?: Record; // 传递给表单元素的额外属性
  events?: Record; // 表单元素事件
  collapsible?: boolean; // 表单项是否可折叠（适用于较低优先级的筛选项）
}

/**
 * 输入框配置接口
 */
export interface InputConfig extends BaseFormItemConfig {
  type: FormItemType.INPUT;
  maxLength?: number;
  minLength?: number;
  showWordLimit?: boolean;
  prefixIcon?: string;
  suffixIcon?: string;
}

/**
 * 选择器配置接口
 */
export interface SelectConfig extends BaseFormItemConfig {
  type: FormItemType.SELECT;
  options: FormItemOption[];
  multiple?: boolean;
  filterable?: boolean;
  remote?: boolean;
  remoteMethod?: (query: string) => void;
  loading?: boolean;
}

/**
 * 日期选择器配置接口
 */
export interface DatePickerConfig extends BaseFormItemConfig {
  type: FormItemType.DATE_PICKER;
  dateType: DatePickerType;
  format?: string;
  valueFormat?: string;
  startPlaceholder?: string;
  endPlaceholder?: string;
  rangeSeparator?: string;
}

/**
 * 单选框组配置接口
 */
export interface RadioGroupConfig extends BaseFormItemConfig {
  type: FormItemType.RADIO_GROUP;
  options: FormItemOption[];
  button?: boolean; // 是否使用按钮样式
}

/**
 * 复选框组配置接口
 */
export interface CheckboxGroupConfig extends BaseFormItemConfig {
  type: FormItemType.CHECKBOX_GROUP;
  options: FormItemOption[];
  min?: number;
  max?: number;
  button?: boolean; // 是否使用按钮样式
}

/**
 * 树选择器配置接口
 */
export interface TreeSelectConfig extends BaseFormItemConfig {
  type: FormItemType.TREE_SELECT;
  data: TreeNode[];
  checkStrictly?: boolean;
  multiple?: boolean;
  checkOnClickNode?: boolean;
  defaultExpandAll?: boolean;
  filterable?: boolean;
  filterMethod?: (query: string, node: TreeNode) => boolean;
}

/**
 * 数字输入框配置接口
 */
export interface NumberConfig extends BaseFormItemConfig {
  type: FormItemType.NUMBER;
  min?: number;
  max?: number;
  step?: number;
  precision?: number;
  controlsPosition?: 'right' | '';
  controls?: boolean;
}

/**
 * 开关配置接口
 */
export interface SwitchConfig extends BaseFormItemConfig {
  type: FormItemType.SWITCH;
  activeText?: string;
  inactiveText?: string;
  activeValue?: boolean | string | number;
  inactiveValue?: boolean | string | number;
}

/**
 * 级联选择器配置接口
 */
export interface CascaderConfig extends BaseFormItemConfig {
  type: FormItemType.CASCADER;
  options: FormItemOption[];
  filterable?: boolean;
  showAllLevels?: boolean;
  multiple?: boolean;
}

/**
 * 自定义表单项配置接口
 */
export interface CustomConfig extends BaseFormItemConfig {
  type: FormItemType.CUSTOM;
  component: Component; // 自定义组件
}

/**
 * 表单项配置联合类型
 */
export type FormItemConfig =
  | InputConfig
  | SelectConfig
  | DatePickerConfig
  | RadioGroupConfig
  | CheckboxGroupConfig
  | TreeSelectConfig
  | NumberConfig
  | SwitchConfig
  | CascaderConfig
  | CustomConfig;

/**
 * 搜索表单配置接口
 */
export interface SearchFormConfig {
  formItems: FormItemConfig[];
  span?: number; // 每个表单项的默认 span
  gutter?: number; // 表单项之间的间距
  labelWidth?: string | number;
  labelPosition?: 'left' | 'right' | 'top';
  inline?: boolean;
  collapsible?: boolean; // 表单是否可折叠
  defaultCollapse?: boolean; // 默认是否折叠
  showResetButton?: boolean; // 是否显示重置按钮
  resetButtonText?: string; // 重置按钮文本
  submitButtonText?: string; // 提交按钮文本
  collapseButtonText?: string; // 折叠按钮文本
  expandButtonText?: string; // 展开按钮文本
}